package com.ktwlsoft.archivemanage.service.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.ktwlsoft.archivemanage.payload.excel.BusinessGuidanceExcel;
import com.ktwlsoft.archivemanage.payload.excel.GuiBusinessGuidanceExcel;
import com.ktwlsoft.archivemanage.payload.excel.GuidanceMendListExcel;
import com.ktwlsoft.archivemanage.payload.excel.MendListExcel;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * ExcelUtils
 *
 * @author zhuhe on 2019/11/6 9:35
 * @version 1.0
 */
public class ExcelUtils {

    /**日志对象*/
    private static Logger logger = LoggerFactory.getLogger(ExcelUtils.class);

    /**
      * 生成整改单Excel文件 on 2019/11/6 9:43
      *
      * @param response response
      * @param mendLists 整改单Excel集合
      */
    public static void generateMendListExcelFile(HttpServletResponse response, List<MendListExcel> mendLists){
        getExcel(response,
                ExcelExportUtil.exportExcel(new ExportParams(),MendListExcel.class,mendLists),
                "检查整改管理",
                ".xlsx");
    }

    /**
     * 生成指导整改Excel文件 on 2019/11/6 9:43
     *
     * @param response response
     * @param mendLists 整改单Excel集合
     */
    public static void generateGuidanceMendListExcelFile(HttpServletResponse response, List<GuidanceMendListExcel> mendLists){
        getExcel(response,
                ExcelExportUtil.exportExcel(new ExportParams(),GuidanceMendListExcel.class,mendLists),
                "指导整改管理",
                ".xlsx");
    }

    /**
     * 生成业务检查Excel文件 on 2019/11/6 9:43
     *
     * @param response response
     * @param businessGuidanceExcels 业务检查Excel集合
     */
    public static void generateBusinessGuidanceExcelFile(HttpServletResponse response, List<BusinessGuidanceExcel> businessGuidanceExcels){
        getExcel(response,
                ExcelExportUtil.exportExcel(new ExportParams(),BusinessGuidanceExcel.class,businessGuidanceExcels),
                "业务检查管理",
                ".xlsx");
    }

    /**
     * 生成业务指导Excel文件 on 2019/11/6 9:43
     *
     * @param response response
     * @param guidanceExcels 业务指导Excel集合
     */
    public static void generateGuidanceBatchExportExcelFile(HttpServletResponse response, List<GuiBusinessGuidanceExcel> guidanceExcels){
        getExcel(response,
                ExcelExportUtil.exportExcel(new ExportParams(),GuiBusinessGuidanceExcel.class,guidanceExcels),
                "业务指导管理",
                ".xlsx");
    }


    /**
      * 获取Excel文件 on 2019/11/6 10:32
      *
      * @param response response
      * @param workbook workbook
      * @param fileNamePrefix fileNamePrefix
      * @param fileNameSuffix fileNameSuffix
      */
    private static void getExcel(HttpServletResponse response,Workbook workbook,String fileNamePrefix,String fileNameSuffix){
        try {
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileNamePrefix,"UTF-8") + fileNameSuffix);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            workbook.write(response.getOutputStream());
        }catch (Exception e){
            e.printStackTrace();
            logger.error("生成Excel文件时发生异常:" + e.getMessage());
        }
    }
}